<template>
  <div class="main">
    <!-- 顶部轮播 -->
    <swiper :options="swiperOption">
      <swiper-slide v-for="(item, index) in bannerData" :key="index">
        <a target="_blank" :href="item.url">
          <img :src="item.image" alt="" srcset="" />
        </a>
      </swiper-slide>
    </swiper>
    <div class="center">
      <!-- 分类 -->
      <div class="classify">
        <el-radio-group
          class="row"
          v-for="(item, index) in classify"
          :key="index"
          v-model="item.name"
          @change="
            (val) => {
              change(item.key, val);
            }
          "
        >
          <el-radio-button
            class="item"
            v-for="(items, index) in item.list"
            :key="index"
            :label="items"
          ></el-radio-button>
        </el-radio-group>
      </div>
      <div class="block-5">
        <div class="body">
          <router-link
            :to="{ name: 'channelDyDetail', query: { id: item.id } }"
            v-for="(item, index) in movieData"
            :key="index"
          >
            <div class="img h370">
              <el-image lazy :src="item.image" alt="" srcset="" />
              <div class="time-grade">
                <p class="time">{{ item.duration }}</p>
                <p class="grade">{{ item.score }}</p>
              </div>
            </div>
            <h1>{{ item.name }}</h1>
            <p>{{ item.detail }}</p>
          </router-link>
        </div>
      </div>
      <!-- 加载更多 -->
      <div class="more-btn" @click="loadMore">加载更多</div>
    </div>
    <!-- 广告 -->
    <div class="ad">
      <a
        target="_blank"
        class="item"
        :href="item.url"
        v-for="(item, index) in adData.slice(0, 5)"
        :key="index"
      >
        <el-image lazy :src="item.image_mini" alt="" />
      </a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 顶部轮播图设置
      swiperOption: {
        autoplay: {
          disableOnInteraction: false, // 用户操作swiper之后，是否禁止autoplay
          delay: 5000, // 自动切换的时间间隔（单位ms）
        },
        // loop: true,
      },
      classify: [],
      adData: [],
      bannerData: [],
      movieData: [], // 电影列表
      //请求参数
      requestData: {
        keyword: "",
        page: 1,
        limit: 25,
      },
    };
  },
  props: ["_keyword"], //接收父组件传值
  // 监听父组件传的_keyword值的变化
  watch: {
    _keyword: function (_keyword) {
      this.requestData.keyword = _keyword;
      this.requestData.page = 1;
      this.movieData = [];
      this.getMovie(this.requestData);
      scrollTo(0, 0);
    },
  },
  created() {
    // 广告
    this.$http.getAd().then((res) => {
      if (res.data.code === 0) {
        this.adData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
    this.requestData.page = 1;
    this.requestData.keyword = "";
    this.getMovie(this.requestData);
    // 顶部轮播
    this.$http.getBanner().then((res) => {
      if (res.data.code === 0) {
        this.bannerData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
    // 分类获取
    this.$http.getSearch({ type: 1 }).then((res) => {
      if (res.data.code === 0) {
        res.data.data.map((item) => {
          item.name = "全部";
          item.list.unshift("全部");
        });
        this.classify = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
  },
  methods: {
    // 分类切换
    change(key, val) {
      this.requestData.page = 1;
      this.requestData[key] = val;
      this.movieData = [];
      if (val === "全部") {
        this.requestData[key] = "";
        this.getMovie(this.requestData);
      } else {
        this.getMovie(this.requestData);
      }
    },
    // 电影列表
    getMovie(data) {
      this.$http.getMovie(data).then((res) => {
        if (res.data.code === 0) {
          if (res.data.data.length <= 0) {
            this.$message.error("没有更多数据！");
            return;
          }
          this.movieData = this.movieData.concat(res.data.data);
        } else {
          this.$message.error("获取失败，请重试！");
        }
      });
    },
    // 加载更多
    loadMore() {
      ++this.requestData.page;
      this.getMovie(this.requestData);
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .swiper-container {
    height: 700px;
    width: 100%;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .el-radio-button__inner {
    border: none;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
    padding: 9px;
    border-radius: 4px;
    background: none;
  }
  .el-radio-button:first-child .el-radio-button__inner {
    border: none;
  }
  .el-radio-button:first-child .el-radio-button__inner,
  .el-radio-button:last-child .el-radio-button__inner {
    border-radius: 4px;
  }
  .el-radio-button__orig-radio:checked + .el-radio-button__inner {
    background: #e5e5e5;
    color: #ff8000;
    box-shadow: none;
    -webkit-box-shadow: none;
    border: none;
  }
}
.center {
  width: 1600px;
  margin: 0 auto;
  .classify {
    .row {
      display: flex;
      margin-top: 20px;
      .item {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #333333;
        padding: 6px 9px;
        border-radius: 4px;
        cursor: pointer;
      }
      .item.active {
        background: #e5e5e5;
        color: #ff8000;
      }
    }
  }
  .h370 {
    height: 370px;
  }
  .block-5 {
    margin-top: 40px;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 45px;
      .lf {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #333333;
      }
      .rt {
        height: 45px;
        display: flex;
        align-items: center;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 40px;
        color: #666666;
        .el-image {
          width: 14.85px;
          height: 25.46px;
          margin-left: 10px;
        }
      }
    }
    .body {
      display: flex;
      flex-wrap: wrap;
      margin-top: 40px;
      a {
        display: inline-block;
        width: 295px;
        margin-bottom: 40px;
        .img {
          position: relative;
          .time-grade {
            width: 100%;
            position: absolute;
            left: 0;
            bottom: 15px;
            padding-left: 10px;
            padding-right: 30px;
            display: flex;
            justify-content: space-between;
            font-family: PingFang SC;
            font-weight: 400;
            .tiem {
              font-size: 20px;
              color: #f5f5f5;
            }
            .grade {
              font-size: 22px;
              color: #ff6f00;
            }
          }
          .el-image {
            width: 100%;
            height: 100%;
            border-radius: 10px;
          }
        }
        h1 {
          font-size: 20px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #333333;
          text-align: right;
          margin: 10px 0;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        p {
          font-size: 16px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 22px;
          color: #999999;
          text-align: right;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .time {
          color: #fff;
        }
      }
      a:not(:nth-child(5n)) {
        margin-right: 31px;
      }
    }
  }
  .more-btn {
    width: 240px;
    height: 60px;
    background: #ffffff;
    border: 1px solid #707070;
    border-radius: 4px;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 60px;
    color: #333333;
    text-align: center;
    margin: 0 auto;
    margin-top: 40px;
    margin-bottom: 80px;
    cursor: pointer;
  }
}
.ad {
  position: fixed;
  top: 150px;
  right: 100px;
  z-index: 1000;
  .item {
    display: block;
    width: 150px;
    height: 150px;
    border-radius: 15px;
    overflow: hidden;
    .el-image {
      width: 100%;
      height: 100%;
    }
  }
  .item:not(:first-child) {
    margin-top: 30px;
  }
}
</style>